'use strict';

// const Controller = require('egg').Controller;
const BaseController = require('../core/base_controller');

class HomeController extends BaseController {
  async index() {
    console.log("home")
    // await ctx.service.UploadService()
    this.ctx.body = '.';
  }

  // async dynamic() {
  //   console.log("home")
  //   // await ctx.service.UploadService()    
  //   this.ctx.body = 'hi, egg';
  // }
  //获取动态 昨天的提现和购买   ,前期需要手工产生数据
  async makeDynamic() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    // console.log("home")
    await ctx.model.Dynamic.create({
      type: params.type,
      content: params.content,
    })
    this.success({}, "创建动态成功");
  }

  /**更新动态
   * id
   */
  async updateDynamic() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    await ctx.model.Dynamic.update({
      type: params.type,
      content: params.content,
    }, {
      where: {
        id: params.id
      }
    })
    this.success({}, "更新成功")
  }
  //获取动态列表
  async getDynamices() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    console.log("home")
    const list = await ctx.model.Dynamic.findAll()
    this.success({
      list: list
    }, "成功");
  }
  //删除动态
  async deleteDynamic() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    console.log("home")
    await ctx.model.Dynamic.destroy({
      where: {
        id: params.id
      }
    })
    this.success({}, "删除成功");
  }


  //发布公告
  async publishNotification() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    // console.log("home")
    await ctx.model.Notification.create({
      title: params.title,
      content: params.content,
      type: params.type,
      version: params.version || ''
    })
    this.success({}, "发布成功");
    // this.ctx.body = 'hi, publishNotification';
  }
  //修改公告
  async updateNotification() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    // console.log("home")
    await ctx.model.Notification.update({
      title: params.title,
      content: params.content,
      type: params.type,
      version: params.version || ''
    }, {
      where: {
        id: params.id
      }
    })
    this.success({}, "发布成功");
    // this.ctx.body = 'hi, publishNotification';
  }

  //删除公告
  async deleteNotification() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    // console.log("home",ctx)
    // console.log("home2",params)
    await ctx.model.Notification.destroy({
      where: {
        id: Number(params.id)
      }
    })
    this.success({}, "删除成功")
  }
  //获取公告
  async getNotification() {
    const {
      ctx
    } = this;
    const params = ctx.request.body;
    console.log("home", params)
    const list = await ctx.model.Notification.findAll({})
    this.success({
      list: list
    }, 'ok')
  }
  //获取公告数量
  async getNotificationCount() {
    const {
      ctx
    } = this;
    // const params = ctx.request.body;
    // console.log("home", params)
    const list = await ctx.model.Notification.findAll({})
    this.success({
      data: list.length
    }, 'ok')
  }
  //获取所有广告
  async getADs() {
    const {
      ctx
    } = this;
     const list = await ctx.model.AD.findAll({
      attributes: {
      // exclude:["updatedAt"]
      }
     })
    this.success({list:list},"ok")
  }
  async publicAD() {
    const {
      ctx
    } = this;
    const p = ctx.request.body;
    if (!p.imgUrl && !p.text) {
      this.fail("图片或文字须传至少一种");
      return
    }
    await ctx.model.AD.create({
      imgUrl: p.imgUrl,
      text: p.text,
      style: p.style
    })
    // console.log(p);
    this.success({}, "发布ok")
  }
  async deleteAD() {
    const {
      ctx
    } = this;
    const p = ctx.request.body;
      await ctx.model.AD.destroy({
        where:{
          id:p.id
        }
      })
      this.success({},"删除ok")
  }
  async updateAD() {
    const {
      ctx
    } = this;
    const p = ctx.request.body;
    if (!p.imgUrl && !p.text) {
      this.fail("图片或文字须传至少一种");
      return
    }
    await ctx.model.AD.update({
      imgUrl: p.imgUrl,
      text: p.text,
      style: p.style
    }, {
      where: {
        id: p.id
      }
    })
    this.success({}, "更新ok")
  }

}

module.exports = HomeController;